package org.tzeng.html.extra;

import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.tzeng.html.bean.DPrice;
import org.tzeng.html.bean.PageInfo;
import org.tzeng.html.bean.TravelInfo;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tzeng on 2016-01-30.
 */
public class TuniuExtra {

    public static PageInfo extraGInfo(String file, TravelInfo travel){
        PageInfo pi = null;

        Document doc = Jsoup.parse(file, "UTF-8");
        String titleStr = doc.title();

        String simple_route = "";
        String fromcity = "", tocity = "";
        if (doc.select("span.citynameforjingzan") != null){
            String citynames = doc.select("span.citynameforjingzan").text();
            simple_route = citynames;
            String[] ch = citynames.split("->");
            fromcity = ch[0] != null ? ch[0] : "";
            if(ch.length-2 < 0){
                tocity = fromcity;
            }else{
                tocity = ch[ch.length-2] != null ? ch[ch.length-2] : fromcity;
            }
        } else if (doc.select("div.m_team_tour_1") != null) {
            fromcity = doc.select("div.m_team_tour_1").attr("data-departcityname");
            tocity = doc.select("div.m_team_tour_1").attr("data-destcityname");
            if (doc.select("div.essentials") != null){
                simple_route = doc.select("div.essentials").text();
            }
        }

        String special_info = "";
        if (doc.select("div.recmark") != null){
            special_info = doc.select("div.recmark").text();
        }



        // days
        Pattern pattern = Pattern.compile("[0-9]+日");
        Matcher matcher = pattern.matcher(titleStr);
        int days = 0;
        if (matcher.find()) {
            days = Integer.parseInt(matcher.group().replaceAll("日", ""));
        } else if (titleStr.indexOf("一日") > 0){
            days = 1;
        }

        String pm_recommend = "";
        if (doc.select("div#recombox ul li div") != null && doc.select("div#recombox ul li div").text().length() > 0){
            pm_recommend = doc.select("div#recombox ul li div").text();
            if(pm_recommend.indexOf("★") > 0)
                pm_recommend = pm_recommend.substring(pm_recommend.indexOf("★")).trim();
        } else if (doc.select("div#recomInnerBox") != null)
            pm_recommend = doc.select("div#recomInnerBox").text();

        pi = new PageInfo();
        pi.setProduct_name(titleStr);
        pi.setProduct_to_city(tocity);
        pi.setProduct_city(fromcity);
        pi.setProvider_href("");
        pi.setProvider_name("");
        pi.setSpecial_info(special_info);
        pi.setSimple_route(simple_route);
        pi.setPm_recommend(pm_recommend);
        pi.setProduct_days(days);
//        pi.setPrices(prices);
        return pi;
    }


    public static String trimNum(String num){
        String result = "";
        for (int i = 0; i < num.length(); i ++){
            if(num.charAt(i)>=48 && num.charAt(i)<=57){
                result+=num.charAt(i);
            }
        }

        return result;
    }
}
